package com.balabala.gym_goods.controller.admin;

import com.balabala.gym_enum.ResultEnum;
import com.balabala.gym_goods.pojo.dto.BaseDeleteDto;
import com.balabala.gym_goods.pojo.dto.CategoryQueryDto;
import com.balabala.gym_goods.pojo.entity.Category;
import com.balabala.gym_goods.pojo.vo.CategoryQueryVo;
import com.balabala.gym_goods.service.CategoryService;
import com.balabala.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@Api(tags = "商品分类表接口")
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "查询所有商品的分类信息")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        List<Category>categoryList=categoryService.list();
        return Result.ok(categoryList);
    }

    @ApiOperation(value = "根据id查询对应的商品分类信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result find(@PathVariable("id") Long id) {
        return  Result.ok(ResultEnum.GENERALLY_SUCCESS,categoryService.getById(id));
    }


    @ApiOperation(value = "添加商品分类信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Category category) {
        log.info("添加商品分类信息");
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        return Result.ok(categoryService.save(category));
    }

    @ApiOperation(value = "根据id删除商品分类信息")
    @RequestMapping (value = "/delete/{id}",method = RequestMethod.GET)
    public Result deleteById(@PathVariable Long id) {
        boolean result=categoryService.removeById(id);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改商品分类信息")
    @RequestMapping (value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody Category category) {
        category.setUpdateTime(new Date());
        boolean result=categoryService.updateById(category);
        return Result.judge(result);
    }

    @ApiOperation("根据商品分类名称模糊查询")
    @RequestMapping (value = "/getCategoryPageList",method = RequestMethod.POST)
    public Result getCategoryPageList(@RequestBody CategoryQueryDto categoryQueryDto){
        PageHelper.startPage(categoryQueryDto.getPage(), categoryQueryDto.getLimit());
        List<CategoryQueryVo> categoryQueryVoList = categoryService.findName(categoryQueryDto);
        PageInfo<CategoryQueryVo> pageInfo = new PageInfo<>(categoryQueryVoList);
        return Result.ok(categoryQueryVoList, pageInfo.getTotal());
    }

    @ApiOperation("批量删除")
    @RequestMapping(value = "/deleteByIds",method = RequestMethod.POST)
    public Result deleteByIds(@RequestBody BaseDeleteDto baseDeleteDto){
        boolean result = categoryService.removeByIds(baseDeleteDto.getIds());
        return Result.ok(result);
    }

}
